package com.jkc.xh.fragments;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jkc.xh.R;
import com.jkc.xh.fragments.core.BaseFragment;
import com.jkc.xh.manager.MoneyManager;
import com.jkc.xh.manager.UserManager;
import com.jkc.xh.models.UserInfo;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.network.models.SystemHintResponse;
import com.jkc.xh.utils.Tool;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.ToastUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 我要提现
 * Created by zhangbreeze on 15/11/21.
 */
@EFragment(R.layout.fragment_pay_refund_cash)
public class PayRefundCashFragment extends BaseFragment {

    @ViewById(R.id.nav_bar)
    NavBar nav_bar;
    @ViewById(R.id.inputCashMoney)
    EditText inputCashMoney;
    /*@ViewById(R.id.inputDesc)
    EditText inputDesc;*/
    @ViewById
    TextView hintInfo;
    @ViewById
    TextView hintInfo2;
    @ViewById
    TextView tip_info;

    @AfterViews
    public void initData() {
        nav_bar.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }
        });
        showProgress("正在加载...");
        addJob(UserManager.getUserInfo(new RestCallBack<UserInfo>() {
            @Override
            public void success(UserInfo userInfo) {
                inputCashMoney.setText(Tool.formatPrice(userInfo.money));
                hideProgress();
            }

            @Override
            public void failure(RestError error) {
                hideProgress();
            }
        }));

        addJob(UserManager.getCpmfHintConfig(new RestCallBack<SystemHintResponse>() {
            @Override
            public void success(SystemHintResponse obj) {
                hintInfo.setText(Html.fromHtml(obj.cash_remark));
                hintInfo2.setText(Html.fromHtml(obj.cash_prompt));
                tip_info.setText(Html.fromHtml(obj.cash_warning));
            }

            @Override
            public void failure(RestError error) {

            }
        }));
    }

    @Click(R.id.btnSave)
    void doSave() {
        String inputMoney = inputCashMoney.getText().toString();
        //String desc = inputDesc.getText().toString();
        if (TextUtils.isEmpty(inputMoney)) {
            ToastUtil.showToastShort(R.string.cash_no_input_hint);
            return;
        }
        showProgress();
        MoneyManager.doRefundCash(inputMoney, "", new RestCallBack<Object>() {
            @Override
            public void success(Object o) {
                closeProgress();
                ToastUtil.showToastShort(R.string.cash_success_hint);
            }

            @Override
            public void failure(RestError error) {
                closeProgress();
                ToastUtil.showToastShort(error.msg);
            }
        });
    }
}
